var basePath = "http://127.0.0.1/GraduationProject";
//var basePath = "http://123.206.33.201/gp/";
var allUser = [];
$(function(){
	
	getAllUser();
	showFixedNav();
	$("#isRightPassword").hide();
	
	// 比较两次输入的密码
	/*$("#secPsw").blur(function(){
		var first = $("#firsePsw").val();
		var sec = $("#secPsw").val();
		if(first == sec && first != "" && sec != ""){
			$("#isRightPassword").show();
		}else{
			$("#inputSuccess2Status").show();
		}
	});
	*/
	
	// 注册
	$("#registeBtn").on("click",function(){
		addUser();
	});
	
	// 登录
	$(".loginBtn").on("click",function(){
		loginUser();
	});
	
	// 取消跳转
	$("#cancelHrefBn").on("click",function(){
		$("#registForm")[0].reset();
		$("#isRightPassword").hide();
	});
});

/**
 * 得到所有注册用户
 */
function getAllUser(){
	$.ajax({
		type:"get",
		url:basePath + "/user/list",
		async:true,
		success:function(data){
			if(data){
				allUser = data.obj.records;
				console.log(allUser);
			}
		}
	});
}

/**
 * 注册用户
 */
function addUser(){
	var first = $("#firsePsw").val();
	var sec = $("#secPsw").val();
	if(first == sec && first != "" && sec != ""){
		var data=$('#registForm').serializeObject();
		console.log(data);
		$.ajax({
			type:"post",
			url:basePath + "/user/add",
			data:data,
			async:true,
			success:function(data){
				if(data){
					if(data.status == "200"){
						$("#myModalLabel").html("注册成功，去登录？");
						$("#myModal").modal("show");
						$("#sureLoginBtn").on("click",function(){
							window.location.href='login.html';
						});
					}
				}
			}
		});
	}else{
		alert("两次密码不一样！请确认密码");
	}
}

/**
 * 用户登录
 */
function loginUser(){
	var data=$('.loginFormIpt').serializeObject();
	console.log(data);
	$.ajax({
		type:"post",
		url:basePath + "/login",
		async:true,
		data:data,
		success:function(data){
			if(data){
				
				if(data.status == "200"){
					var dataAttr = [];
					dataAttr.push(data.obj);
					window.location.href='indexPage.html';
				}else if(data.status == "500"){
					$("#returnInfo").show();
				}
				
			}
		}
	});
	
}


/**
 * 显示固定导航
 */
function showFixedNav(){
	var last_scroll_position;
	$("#fixedNav").hide();

	window.addEventListener('scroll', function(e) {
	  last_scroll_position = window.scrollY;

	  if (last_scroll_position > 200 ) {
		$("#fixedNav").fadeIn();

	  } else if (last_scroll_position < 200) {
		$("#fixedNav").hide();
	  }
	});
}


/**
 * 将form提交的数据转化成形如{name:value...}的json
 */
$.fn.serializeObject = function () {
    var o = {};

    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });

    return o;
};

/**
 * 验证注册时两次密码是否相同
 */
function validatePsw(){
	
	$("#secPsw").blur(function(){
		var firstVal = $("#firsePsw").val();
		var secVal = $("#secPsw").val();
		
		if(firstVal != secVal){
			alert("两次输入密码不同");
		}
	});
	
}

/**
 * 将form提交的数据转化成形如{name:value...}的json
 */
$.fn.serializeObject = function () {
    var o = {};

    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });

    return o;
};





